package com.lovely602.ai.openai.controller.chat;


import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 *
 * @author lizhixing
 */
@RestController
@RequestMapping("/ai")
public class ChatAiController {

    @Autowired
    private ChatClient chatClient;

    // 角色预设，使用非流式响应
    @GetMapping(value = "/chat/call")
    public String chatCall(@RequestParam(value = "msg", required = true) String msg) {
        return chatClient.prompt()
                .user(msg) // 用户输入
                .call()    // 使用非流式响应
                .content();
    }

    // 角色预设，使用流式响应
    @GetMapping(value = "/chat/stream", produces = "text/html;charset=UTF-8")
    public Flux<String> chatStream(@RequestParam(value = "msg", required = true) String msg) {
        return chatClient.prompt()
                .user(msg) // 用户输入
                .stream()  // 使用流式响应
                .content();
    }

}
